# -*- coding: utf-8 -*-
"""User models."""
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
from werkzeug.security import generate_password_hash, check_password_hash
from flask_login import UserMixin
from flask import current_app
import datetime as dt

from myflaskapp.database import Column, Model, SurrogatePK, db, reference_col, relationship

class Project(Model,SurrogatePK):
    __tablename__ = 'projects'
    name = Column(db.String(64),unique=True, index=True)
    content = Column(db.String(300))
    update_time = Column(db.DateTime, nullable=False, default=dt.datetime.utcnow)
    start_time = Column(db.DateTime, nullable=False, default=dt.datetime.utcnow)
    stages = relationship('Stage',backref='project')
    problems = relationship('Problem',backref='project')
    post = relationship('Post',backref='project')
    logs = relationship('Log',backref='project')
    users = relationship('Relation_user')

    def __init__(self, name, **kwargs):
        """Create instance."""
        db.Model.__init__(self, name=name, **kwargs)

    def __repr__(self):
        return '<Project %r>' % self.name

class Relation_user(Model,SurrogatePK):
    __tablename__ = 'relation_users'
    username = Column(db.String(10))
    username_id = Column(db.String(64))
    project_id = reference_col('projects', nullable=True)
    
    def __init__(self, username, **kwargs):
        """Create instance."""
        db.Model.__init__(self, username=username, **kwargs)

    def __repr__(self):
        return '<Relation_user %r>' % self.username

class Stage(Model,SurrogatePK):
    __tablename__ = 'stages'
    item = Column(db.String(64))
    content = Column(db.String(300))
    project_id = reference_col('projects', nullable=True)
    sequence = Column(db.SmallInteger)
    works = relationship('Work', backref='stage')
    state = Column(db.String(32))

    def __init__(self, item, **kwargs):
        """Create instance."""
        db.Model.__init__(self, item=item, **kwargs)

    def __repr__(self):
        return '<State %r>' % self.item

class Work(Model,SurrogatePK):
    __tablename__ = 'works'
    item = Column(db.String(64))
    content = Column(db.String(300))
    stage_id = reference_col('stages', nullable=True)
    state = Column(db.String(32))
    update_time = Column(db.DateTime, nullable=False, default=dt.datetime.utcnow)
    start_time = Column(db.DateTime, nullable=False, default=dt.datetime.utcnow)
    files = relationship('File',backref='work')
    contents = relationship('Content',backref='work')
    username = Column(db.String(10))

    def __init__(self, item, **kwargs):
        """Create instance."""
        db.Model.__init__(self, item=item, **kwargs)

    def __repr__(self):
        return '<Work %r>' % self.item

class Content(Model,SurrogatePK):
    __tablename__ = 'contents'
    name = Column(db.String(100))
    leixing = Column(db.SmallInteger)
    complete = Column(db.SmallInteger)
    work_id = reference_col('works', nullable=True)

    def __init__(self, name, **kwargs):
        db.Model.__init__(self, name=name, **kwargs)

    def __repr__(self):
        return '<Content %r>' % self.name

class Problem(Model,SurrogatePK):
    __tablename__ = 'problems'
    item = Column(db.String(64))
    content = Column(db.String(300))
    start_time = Column(db.DateTime, nullable=False, default=dt.datetime.utcnow)
    discussions = relationship('Discussion', backref='problem')
    state = Column(db.String(32))
    username = Column(db.String(10))
    project_id = reference_col('projects',nullable=True)

    def __init__(self, item, **kwargs):
        """Create instance."""
        db.Model.__init__(self, item=item, **kwargs)

    def __repr__(self):
        return '<Problem %r>' % self.item
    
class Discussion(Model,SurrogatePK):
    __tablename__ = 'discussions'
    content = Column(db.String(500))
    create_time = Column(db.DateTime, nullable=False, default=dt.datetime.utcnow)
    username = Column(db.String(10))
    problem_id = reference_col('problems',nullable=True)

    def __init__(self, content, **kwargs):
        """Create instance."""
        db.Model.__init__(self, content=content, **kwargs)

    def __repr__(self):
        return '<Disscussion %r>' % self.content

class Post(Model,SurrogatePK):
    __tablename__ = 'posts'
    title = Column(db.String(64))
    content = Column(db.Text)
    create_time = Column(db.DateTime, nullable=False, default=dt.datetime.utcnow)
    username = Column(db.String(10))
    project_id = reference_col('projects', nullable=True)

    def __init__(self, title, **kwargs):
        """Create instance."""
        db.Model.__init__(self, title=title, **kwargs)

    def __repr__(self):
        return '<Post %r>' % self.title

class File(Model,SurrogatePK):
    __tablename__ = 'files'
    title = Column(db.String(64))
    url = Column(db.String(200))
    create_time = Column(db.DateTime, nullable=False, default=dt.datetime.utcnow)
    work_id = reference_col('works', nullable=True)

    def __init__(self, title, **kwargs):
        """Create instance."""
        db.Model.__init__(self, title=title, **kwargs)

    def __repr__(self):
        return '<File %r>' % self.title

class Log(Model,SurrogatePK):
    __tablename__ = 'logs'
    log = Column(db.String(100))
    create_time = Column(db.DateTime, nullable=False, default=dt.datetime.utcnow)
    project_id = reference_col('projects', nullable=True)
    workid = Column(db.Integer, nullable=True)

    def __init__(self, log, **kwargs):
        db.Model.__init__(self, log=log, **kwargs)
    
    def __repr__(self):
        return '<Log %r>' % self.log

class Institution(Model,SurrogatePK):
    __tablename__ = 'institutions'
    name = Column(db.String(64))
    content = Column(db.Text)
    leibie = Column(db.String(64))

    def __init__(self, name, **kwargs):
        db.Model.__init__(self, name=name, **kwargs)
    
    def __repr__(self):
        return '<Institution %r>' % self.name